package algo.math.evaluator.context.operator;

import algo.math.evaluator.base.IBase;
import algo.math.evaluator.base.exceptions.InvalidToken;
import algo.math.evaluator.base.exceptions.UnrecognizedToken;
import algo.math.evaluator.exceptions.InvalidOperationException;
import algo.math.evaluator.value.IValue;

/**
 * Represent an standard arithmetic operator (+,-,/,*)
 */
public abstract class OperatorPrimitive extends Operator {

	
	/**
	 * Helper function used to format values for further processing
	 * @return Buffered integer and decimal values to match each other size.
	 */
	protected String[] prepare (IValue nb1, IValue nb2, IBase b)
	{
		IValue sup= nb1;
		IValue inf= nb2;
		
			if (nb1.compareTo(nb2)== IValue.Compare.INFERIOR)
				{
						 sup= nb2;
						 inf= nb1;
				}
			
			String int1 =sup.getIntegerValue();
			String int2 =inf.getIntegerValue();
			
			String dec1 =sup.getDecimalValue();
			String dec2 =inf.getDecimalValue();
			
			if (int1.length()>int2.length())
			{
				int l  =int1.length()-int2.length();
				for (int i = 0;i< l;i++)
				{
					int2= b.valueOf(0)+ int2;
				}
			}
			
			
			if (dec1.length()>dec2.length())
			{
				int l  =dec1.length()-dec2.length();
				
				for (int i = 0;i< l;i++)
					{
					dec2= dec2 + b.valueOf(0);
					}
			}
			else if (dec2.length()>dec1.length())
			{
				int l  =dec2.length()-dec1.length();
				
				for (int i = 0;i< l;i++)
					{
					dec1=  dec1 + b.valueOf(0);
					}
			}
			
			return new String [] {int1,int2,dec1,dec2};
			
	}
	
	/**
	 * Activate the operator on the selected operands to produce a value.
	 *
	 * @param operands Operands handle by the operator
	 */
	protected abstract IValue resolve (IValue[] operands) throws UnrecognizedToken, InvalidToken, InvalidOperationException;

	
}
